﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'layer', 'table', 'laypage', 'laydate', 'jquery'], function () {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer,
        laydate = layui.laydate;

    var salaryListLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),//异步方法管理
                title: [],
                field: 'Name',
                order: 'asc',
                projectAllList: "/Project/Info/LoadProjectAllList",
                listUrl: "/Staff/Salary/LoadEmployeeSalaryList",
                settlementInfoUrl: "/Staff/Salary/SettlementInfo"
            };

            this.Parameters.ac
                //.pushQueue(function () {
                //    CommonLogic.loadButtonsInfo(function () {
                //        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                //    })
                //})
                .pushQueue(function () {
                    self.logicFunc.loadProjectAllList(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();

            //this.initPage();
            //this.bindEvent();
        },
        //初始化列表
        initPage: function () {

            //日期
            var lastMoth = new Date(new Date().setMonth(new Date().getMonth() - 1));
            var startTime = laydate.render({
                elem: '#txtBeginTime',
                format: 'yyyy-MM-dd',
                value: lastMoth,
                done: function (value, date, endDate) {
                    endLayDate.config.min = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                    };
                }
            });

            var endLayDate = laydate.render({
                elem: '#txtEndTime',
                format: 'yyyy-MM-dd',
                value: new Date(),
                done: function (value, date, endDate) {
                    startTime.config.max = {
                        year: date.year != undefined ? date.year : 2099,
                        month: (date.month != undefined) ? (date.month - 1) : 12,
                        date: (date.date != undefined) ? date.date : 25
                    };
                }
            });

            //获取用户信息
            var title =
                [
                    { type: 'radio', fixed: 'left' },
                    { field: 'ID', title: 'ID', hide: true },
                    { field: 'Name', title: '姓名', width: 130, align: "center", fixed: 'left' },
                    {
                        field: 'Sex', title: '性别', width: 80, align: "center", fixed: 'left',
                        templet: function (d) {
                            return GrdEnum.getValue.userSexValue(d.Sex.trim());
                        }
                    },
                    { field: 'WorkTypeName', title: '工种', width: 110, align: "center", fixed: 'left' },
                    { field: 'MName', title: '类别', width: 90, align: "center", fixed: 'left' },

                    { field: 'LoanMoney', title: '借款金额', width: 90, align: "center" },
                    { field: 'RealMoney', title: '实发工资', width: 90, align: "center" }, 

                    {
                        field: 'WorkDay', title: '已审出勤', width: 90, align: "center", 
                        templet: function (d) {
                            return '<a class="" style="color:#ED2A4A">' + d.WorkDay+'</a>';
                        }
                    },
                    {
                        field: 'NoWorkDay', title: '未审出勤', width: 90, align: "center", 
                        templet: function (d) {
                            return '<a class="" style="color:#ED2A4A">' + d.NoWorkDay + '</a>';
                        }
                    },
                    {
                        field: 'WorkDays', title: '出勤合计', width: 90, align: "center",
                        templet: function (d) {
                            return '<a class="" style="color:#ED2A4A">' + (d.NoWorkDay + d.WorkDay) + '</a>';
                        }
                    },
                    {
                        field: 'OverTime', title: '加班小时', width: 90, align: "center",
                        templet: function (d) {
                            return '<a class="" style="color:#35B2FF">' + d.OverTime + '</a>';
                        }
                    },
                    {
                        field: 'TOverMoney', title: '加班工资', width: 90, align: "center",
                        templet: function (d) {
                            return '<a class="" style="color:#35B2FF">' + d.TOverMoney + '</a>';
                        }
                    },
                    {
                        field: 'AmountMoney', title: '计件工资', width: 90, align: "center",
                        templet: function (d) {
                            return '<a class="" style="color:#B883F2">' + d.AmountMoney + '</a>';
                        }
                    },
                    { field: 'Code', title: '身份证号', width: 170, align: "center" },
                    { field: 'BankNo', title: '银行账号', width: 170, align: "center" },
                    //{ field: 'BankName', title: '开户银行', width: 120, align: "center" },
                    { field: 'Phone', title: '联系电话', width: 120, align: "center" }, 
                    { field: 'opt', title: '操作', toolbar: '#gridTool', width: '90', align: 'left', fixed: 'right' }
                ]; 


            //table加载
            GrdLayUI.table({
                dom: '#InfoList',
                radio: true,
                url: self.Parameters.listUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                height: "full-140",
                parameters: {
                    ProjectNo: $("#ProjectNo").val(),
                    Name: $("#Name").val(),
                    PYM: $("#PYM").val(),
                    dBeginTime: $("#txtBeginTime").val(),
                    dEndTime: $("#txtEndTime").val()
                },
                title: title
            });

            //排序监听
            GrdLayUI.tableSort({
                domId: "InfoList",
                backAction: function (obj) {
                    self.Parameters.field = obj.field;
                    self.Parameters.order = obj.type;
                    self.logicFunc.loadListInfo();
                }
            });
            form.render();
        },
        //绑定事件
        bindEvent: function () {

            //监听(列表操作)
            table.on('tool(InfoList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnSettlement":
                        self.logicFunc.settlementInfo(data);
                        break;
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //结算工资
            $("#funBtnSettlement").on("click", function () {
                self.logicFunc.settlementInfo();
            });
        },
        /*
         * 逻辑方法
         */
        logicFunc: {
            //获取所有的工程信息
            loadProjectAllList: function (func) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectAllList,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#ProjectNo").empty();
                            $("#ProjectNo").append("<option value=''>所有工程项目</option>");
                            for (var i = 0; i < result.Data.length; i++) {
                                $("#ProjectNo").append("<option value=" + result.Data[i].ProjectNo + ">" + result.Data[i].ProjectName + "</option>");
                            }
                        }
                        if (func) {
                            func();
                        }
                    }
                });
            },

            ///获取数据
            loadListInfo: function () {

                GrdLayUI.tableReload({
                    domId: "InfoList",
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        Name: $("#Name").val(),
                        PYM: $("#PYM").val(),
                        dBeginTime: $("#txtBeginTime").val(),
                        dEndTime: $("#txtEndTime").val()
                    }
                });
            },

            //结算工资
            settlementInfo: function (data) {

                if (data != undefined && data != null) {

                    GrdLayUI.setUrlParameters(data);

                    var index = layui.layer.open({
                            title: "结算工资",
                            type: 2,
                            resize: false,
                            area: ['500px', '500px'],
                            content: self.Parameters.settlementInfoUrl,
                            end: function () {
                                $(window).unbind("resize");
                            }
                    });

                    //layui.layer.full(index);
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "InfoList",
                        backAction: function (data) {

                            GrdLayUI.setUrlParameters(data);

                            var index = layui.layer.open({
                                    title: "结算工资",
                                    type: 2,
                                    resize: false,
                                    area: ['500px', '500px'],
                                    content: self.Parameters.settlementInfoUrl,
                                    end: function () {
                                        $(window).unbind("resize");
                                    }
                            });

                            //layui.layer.full(index);
                        }
                    });
                }
            },
        },
        //工具
        tools: {

        }
    };
    salaryListLogic.init();
});
